package com.gs.disk.modules.headImgAndWallper.dao.mobile;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.common.mapper.CommonMapper;
import com.gs.common.util.ConvertUtils;
import com.gs.disk.modules.headImgAndWallper.dto.APairResourceDto;
import com.gs.disk.modules.headImgAndWallper.dto.ResourceDto;
import com.gs.disk.modules.headImgAndWallper.entity.APairResourceEntity;
import com.gs.disk.modules.headImgAndWallper.entity.ResourceEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
public interface MobileAPairResourceDao extends CommonMapper<APairResourceEntity, APairResourceEntity> {

    /**
     * 获取所有资源列表
     * @param categoryName
     * @return
     */
    default Map<String, List<APairResourceDto>> aPairResourceListMap(String categoryName) {
        // 获取资源列表
        List<APairResourceDto> resourceDtoList = ConvertUtils.sourceToTarget(aPairResourceList(categoryName), APairResourceDto.class);
        Map map = new HashMap();
        resourceDtoList.forEach(r -> {
            Object categoryList = map.get(r.getCategoryName());

            // 不存在就创建, 存在就添加
            if(ObjectUtil.isNull(categoryList)) {
                List<APairResourceDto> list = new ArrayList();
                list.add(r);
                map.put(r.getCategoryName(), list);
            }else {
                ((List)categoryList).add(r);
                map.put(r.getCategoryName(), categoryList);
            }
        });
        return map;
    }

    /**
     * 获取资源列表
     * @param categoryName
     * @return
     */
    default List<APairResourceEntity> aPairResourceList(String categoryName) {
        QueryWrapper<APairResourceEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, category_id,category_name,head_img_id,background_img_id,nickname,keyword");

        if(ObjectUtil.isNotEmpty(categoryName)) {
            wrapper.eq("category_name", categoryName);
        }
        return this.selectList(wrapper);
    }
}
